/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.search;

import javax.servlet.*;
import java.util.*;
import bw.util.*;

public class SearchServant implements Servant
{
    public void init ( ServletContext context )
    {
        //add the search queries
        //maybe this data should be read from a config file?
        //doing it this way to make it easy to convert in the future
        Hashtable q = new Hashtable();
        q.put( "am", "http://www.amazon.com/exec/obidos/external-search/?mode=books&keyword=" );
        //q.put( "df", "http://dictionary.reference.com/search?q=" );
        q.put( "df", "http://ninjawords.com/" );
        q.put( "th", "http://thesaurus.reference.com/search?q=" );
        q.put( "imdb", "http://www.imdb.com/find?s=all&q=" );
        q.put( "mrqe", "http://www.mrqe.com/lookup?" );
        q.put( "wkp", "http://en.wikipedia.org/wiki/Special:Search?go=Go&search=" );
        q.put( "map", "http://maps.google.com/maps?q=" );
        q.put( "img", "http://images.google.com/images?hl=en&btnG=Search+Images&q=" );

        SearchManager manager = SearchManager.getInstance();

        for ( Enumeration e = q.keys(); e.hasMoreElements(); )
        {
            String code = (String) e.nextElement();
            String path = (String) q.get( code );
            manager.addSearchQuery( code, new GenericSearchQuery( code, path ) );
        }

        //set up the map quest search query
        String mapQuestStartStr = context.getInitParameter( "mapquestStartLocation" );
        
        //construct the start location. 
        StringTokenizer st = new StringTokenizer( mapQuestStartStr, "," );
        Location startLoc = new Location();
        try
        {
            //this is massively sucky! REVISIT
            startLoc.street = st.nextToken().trim();
            startLoc.city = st.nextToken().trim();
            startLoc.state = st.nextToken().trim();
            startLoc.zip = st.nextToken().trim();
        }
        catch ( Exception ex )
        {
            Log.getInstance().write( "Problem parsing MapQuest start location!" );
        }

        String googleMapsKey = context.getInitParameter( "googleMapsKey" );
        Geocoder geocoder = new Geocoder( googleMapsKey );

        MapQuestSearchQuery mq = new MapQuestSearchQuery( geocoder );
        mq.setStartLocation( startLoc );

        manager.addSearchQuery( "mq", mq );

        //add the WebDash Command query
        manager.addSearchQuery( "wd", new CommandQuery() );

        //set the default query
        GenericSearchQuery google = new GenericSearchQuery( null, 
                                                            "http://www.google.com/search?q=" );
        manager.setDefaultQuery( google );
    }

    public void shutdown ( ServletContext context )
    {
        //nothing
    }
}